package com.yugao.fintech.antelope.mps.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.mps.mapper.MsgHistoryMapper;
import com.yugao.fintech.antelope.mps.mapper.MsgPushTaskMapper;
import com.yugao.fintech.antelope.mps.model.entity.MsgHistory;
import com.yugao.fintech.antelope.mps.model.entity.MsgPushTask;
import com.yugao.fintech.antelope.mps.model.rqrs.history.HistoryPageReq;
import com.yugao.fintech.framework.assistant.utils.CollectionUtils;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import com.yugao.fintech.framework.mybatisplus.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 消息推送 仓储层
 */
@Slf4j
@Repository
public class MsgHistoryManager extends ServiceImpl<MsgHistoryMapper, MsgHistory>
        implements IService<MsgHistory> {

    @Autowired
    private MsgPushTaskMapper pushTaskMapper;


    public void deleteHistoryByTaskIds(List<Long> taskIds) {
        if (CollectionUtils.isEmpty(taskIds)) {
            return;
        }
        LambdaQueryWrapper<MsgHistory> lqw = MsgHistory.lqw().in(MsgHistory::getTaskId, taskIds);
        this.remove(lqw);
    }

    public void deleteHistoryByTemplateIds(List<Long> templateIds) {
        if (CollectionUtils.isEmpty(templateIds)) {
            return;
        }
        LambdaQueryWrapper<MsgHistory> lqw = MsgHistory.lqw().in(MsgHistory::getTemplateId, templateIds);
        this.remove(lqw);
    }

    /**
     * 分页查询
     *
     * @return 分页数据
     */
    public IPage<MsgHistory> pageHistory(HistoryPageReq req) {
        LocalDateTime beginDateTime = req.getBeginLocalDateTime();
        LocalDateTime endDateTime = req.getEndLocalDateTime();
        Long groupId = req.getGroupId();
        List<Long> taskIds = Collections.emptyList();
        if (Objects.nonNull(groupId)) {
            LambdaQueryWrapper<MsgPushTask> lqw = MsgPushTask.lqw().eq(MsgPushTask::getGroupId, req.getGroupId());
            List<MsgPushTask> pushTasks = pushTaskMapper.selectList(lqw);
            taskIds = pushTasks.stream().map(MsgPushTask::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(taskIds)) {
                taskIds = new ArrayList<Long>() {{
                    add(-1L);
                }};
            }
        }

        LambdaQueryWrapper<MsgHistory> queryWrapper = Wrappers.<MsgHistory>lambdaQuery()
                .eq(Objects.nonNull(req.getTaskId()), MsgHistory::getTaskId, req.getTaskId())
                .eq(StringUtils.isNotEmpty(req.getStatus()), MsgHistory::getStatus, req.getStatus())
                .eq(StringUtils.isNotEmpty(req.getChannelType()), MsgHistory::getChannelType, req.getChannelType())
                .eq(StringUtils.isNotEmpty(req.getToUser()), MsgHistory::getToUser, req.getToUser())
                .in(CollectionUtils.isNotEmpty(taskIds), MsgHistory::getTaskId, taskIds)
                .eq(StringUtils.isNotEmpty(req.getBizType()), MsgHistory::getBizType, req.getBizType())
                .ge(Objects.nonNull(beginDateTime), MsgHistory::getPushTime, beginDateTime)
                .le(Objects.nonNull(endDateTime), MsgHistory::getPushTime, endDateTime)
                .orderByDesc(MsgHistory::getPushTime);
        return this.page(PageUtil.to(req), queryWrapper);
    }
}